package com.itheima.service.Impl.Dept;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Dept;
import com.itheima.mapper.store.company.CompanyMapper;
import com.itheima.mapper.system.dept.DeptMapper;
import com.itheima.service.DeptService;
import com.itheima.utils.IDutils;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl implements DeptService {
    @Override
    public List<Dept> findAll(String page, String pageSize) {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        int page11=1;
        int pageSize22=10;
        if(page!=null){
            page11 = Integer.parseInt(page);
        }
        if (pageSize!=null){
            pageSize22=Integer.parseInt(pageSize);
        }
        PageHelper.startPage(page11,pageSize22);
        List<Dept> list=  mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public List<Dept> findAlldept() {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        List<Dept> list = mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public void save(Dept dept) {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        dept.setId(IDutils.getUUId());
        mapper.save(dept);
        session.commit();
        MapperUtil.close(session);

    }

    @Override
    public Dept edit(String id) {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        Dept dept= mapper.edit(id);
        MapperUtil.close(session);
        return dept;

    }

    @Override
    public void update(Dept dept) {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);

        mapper.update(dept);

        session.commit();
        MapperUtil.close(session);

    }

    @Override
    public void delete(String id) {
        SqlSession session = MapperUtil.getSession();
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        String[] id1 = id.split(",");
        mapper.delete(id1);
        session.commit();
        MapperUtil.close(session);
    }
}
